using System;
using System.Collections;
using System.Data.OracleClient;
using System.Reflection;

namespace Prologis.ShifterNT.Datasource
{

    public class OracleDataSource : DataSourceSkeleton
    {

        OracleConnection connection;
        OracleDataReader dataReader;
        OracleTransaction transaction;

        int recordCount = 0;


        /// <summary>
        /// 
        /// </summary>
        /// 
        string connectionString;
        public string ConnectionString
        {
            get { return connectionString; }
            set { connectionString = value; }
        }


        /// <summary>
        /// 
        /// </summary>
        /// 
        string tableName;
        public string TableName
        {
            get { return tableName; }
            set { tableName = value; }
        }


        /// <summary>
        /// Gets or sets the max records.
        /// Sample: <property name="MaxRecords" value="5000" />
        /// </summary>
        /// <value>The max records.</value>
        int maxRecords = int.MaxValue;
        public int MaxRecords
        {
            get { return maxRecords; }
            set { maxRecords = value; }
        }



        /// <summary>
        /// 
        /// </summary>
        /// 
        public override bool Active
        {
            set
            {
                if (base.Active != value)
                {
                    base.Active = value;
                    if (value)
                    {
                        connection = new OracleConnection(connectionString);
                        OracleCommand command = new OracleCommand("select * from " + tableName, connection);
                        connection.Open();
                        transaction = connection.BeginTransaction();
                        command.Transaction = transaction;
                        Count = GetRecordCount();
                        dataReader = command.ExecuteReader();
                    }
                    else
                    {
                        transaction.Commit();
                        connection.Close();
                    }
                }
            }
        }


        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        /// 
        public int GetRecordCount()
        {
            OracleCommand command = new OracleCommand("select count(*) from " + tableName, connection);
            command.Transaction = transaction;
            OracleDataReader dr = command.ExecuteReader();
            dr.Read();
            return int.Parse(dr.GetValue(0).ToString());
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="fieldList"></param>
        /// <returns></returns>
        /// 
        public override MoveNextDecision GetNext(ref FieldList fieldList)
        {
            if (dataReader.Read())
            {
                if (recordCount++ > maxRecords)
                {
                    return MoveNextDecision.EOS;
                }
                else
                {
                    fieldList = Tokenizer.Tokenize(String.Empty, StringArrayBuilder.GetArray(dataReader));
                    return base.GetNext(ref fieldList);
                }
            }
            else
            {
                return MoveNextDecision.EOS;
            }
        }


    }
}

